package com.lindroy.androidplayer.ui.search

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.google.android.flexbox.*
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.databinding.FragmentSearchHotKeyBinding
import com.lindroy.androidplayer.logic.network.base.http.observeResponse
import com.lindroy.androidplayer.ui.adapter.SearchHistoryAdapter
import com.lindroy.androidplayer.ui.adapter.SearchHotKeyAdapter
import com.lindroy.androidplayer.utils.KVUtil
import com.lindroy.androidplayer.utils.collectWhenResumed
import com.lindroy.androidplayer.viewmodels.SearchHotKeyViewModel
import com.lindroy.androidplayer.viewmodels.SearchViewModel

/**
 * @author Lin
 * @date 2022/3/9
 * @function
 */
class SearchHotKeyFragment : BaseLazyFragment<FragmentSearchHotKeyBinding>() {

    private val searchViewModel by activityViewModels<SearchViewModel>()

    private val viewModel by viewModels<SearchHotKeyViewModel>()

    private val hotkeyAdapter by lazy { SearchHotKeyAdapter(viewLifecycleOwner) }

    private val historyAdapter by lazy { SearchHistoryAdapter(viewLifecycleOwner) }

    private val layoutManager: FlexboxLayoutManager
        get() = FlexboxLayoutManager(requireContext()).apply {
            flexWrap = FlexWrap.WRAP
            flexDirection = FlexDirection.ROW
            justifyContent = JustifyContent.FLEX_START
            alignItems = AlignItems.CENTER
        }

    override fun FragmentSearchHotKeyBinding.initView(savedInstanceState: Bundle?) {
        vm = viewModel
        rvHotKey.layoutManager = layoutManager
        rvHotKey.adapter = hotkeyAdapter
        rvHistory.layoutManager = layoutManager
        rvHistory.adapter = historyAdapter
    }

    override fun FragmentSearchHotKeyBinding.initListener() {
        viewModel.hotKeysResp.data.observeResponse(viewLifecycleOwner) { list ->
            hotkeyAdapter.setNewData(list)
        }
        hotkeyAdapter.setOnChildClickListener { view, item, position ->
            toSearchPage(item.name)
        }
        viewModel.searchHistory.observe(viewLifecycleOwner) {
            historyAdapter.setNewData(it)
        }
        historyAdapter.setOnChildClickListener { view, item, position ->
            toSearchPage(item.keyword)
            viewModel.getSearchHistory()
        }
        historyAdapter.setOnRemoveItemListener {
            viewModel.removeHistoryItem(it)
        }
        searchViewModel.startSearchListener.collectWhenResumed(viewLifecycleOwner) {
            //重新获取数据
            viewModel.getSearchHistory()
        }
    }

    private fun toSearchPage(keyword: String?) {
        if (keyword == null) {
            return
        }
        KVUtil.addSearchHistory(keyword)
        searchViewModel.keyword.postValue(keyword)
        SearchResultFragment.start(this@SearchHotKeyFragment, keyword)
    }

    override fun lazyLoad() {
        viewModel.hotKeysResp.load()
        viewModel.getSearchHistory()
    }
}